// pages/operat/fleedTools/fillThebox/transferGoodsApply/transferGoodsApply.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AdjustCargoModel: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = LApp.pages.get("fleedTools/fillThebox/scanCode/scanCode").data;
    this.setData({
      AdjustCargoModel: data.info
    })
    if (this.data.AdjustCargoModel.ProductID != 0) {
      this.productShow(this.data.AdjustCargoModel.ProductID);
    }
    else
      this.getproductList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  deal: function () {
    LApp.openView('transferGoodsApply/transferGoodsApply');
  },
  GetFormCode: function (data) {
    var formCode = LApp._formatTime("yyyyMMddhhmmss");
    if (data) {
      formCode = data + "-" + formCode;
    }
    this.data.AdjustCargoModel.FormCode = formCode;
    this.setData(this.data);
  },
  getproductList: function () {
    let _this = this;
    LApp._get({
      url: 'nbapi/product/pselectitem',
      data: { userId: LApp.globalData.userInfo.UserID },
      success: function (r) {
        _this.setData({
          productList: r
        })
      }
    })
  },
  productShow: function (proId) {
    let _this = this;
    LApp._get({
      url: 'nbapi/product/showproductinfo',
      data: { proId: proId },
      success: function (res) {
        _this.data.AdjustCargoModel.pName = res.ProductName;
        _this.data.AdjustCargoModel.ProductID = res.Id;
        _this.setData(_this.data);
        var dId = LApp.globalData.userInfo.Distributor;
        _this.GetFormCode(dId + "-" + res.Id);
      }
    })

  },
  bindModelChange: function (e) {
    wx.$$apply(e, this.data.outStorageModel);
  },
  save:function()
  {
   if (this.data.AdjustCargoModel.ProductID == undefined || this.data.AdjustCargoModel.ProductID == 0) {
    wx.showModal({
     title: '提示',
     content: '请选择产品',
     showCancel: false
    })
    return;
   }
    this.data.AdjustCargoModel.CreateLoginID = LApp.globalData.userInfo.UserID;
    this.data.AdjustCargoModel.CreateLoginName = LApp.globalData.userInfo.LoginName;
    this.data.AdjustCargoModel.EnterpriseID = LApp.globalData.userInfo.EnterpriseId;    
    LApp._post({
      url: 'nbapi/fleedtool/adjustcargo',
      data:this.data.AdjustCargoModel,
      success: function (r) {
        wx.showModal({
          title: '保存成功！',
          icon: "success",
          success: function (res) {
            if (!res.cancel) { 
              var pages = getCurrentPages();
              var currPage = pages[pages.length - 1];   //当前页面
              var prevPage = pages[pages.length - 2];  //上一个页面
              prevPage.setData({
                myDate: 1
              });
              wx.navigateBack(1);
            }
          }
        })
      }
    })
  },
  goSearchCode: function () {
   if (this.data.AdjustCargoModel) {
    LApp.pages.add(this);
    wx.navigateTo({
     url: 'searchCode/searchCode',
    })
   }
  }
})